package com.leyou.auth.config;

import com.auth.utils.JwtUtils;
import com.leyou.auth.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author 虎哥
 */
@Configuration
@ConditionalOnProperty(prefix = "ly.auth", name = "enable", havingValue = "true")
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    @Lazy
    private JwtUtils jwtUtils;
    @Autowired
    private ClientProperties prop;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor(jwtUtils));
        // 添加拦截路径
        List<String> includePathPatterns = prop.getIncludeFilterPaths();
        if(!CollectionUtils.isEmpty(includePathPatterns)){
            registration.addPathPatterns(includePathPatterns);
        }
        // 添加排除路径
        List<String> excludePathPatterns = prop.getExcludeFilterPaths();
        if (!CollectionUtils.isEmpty(excludePathPatterns)) {
            registration.excludePathPatterns(excludePathPatterns);
        }
    }
}
